﻿<!DOCTYPE html>
<html class="theme theme-white">
<head>
<meta charset="utf-8">
<title>简单工厂设计模式</title>
<link href="https://www.zybuluo.com/static/assets/template-theme-white.css" rel="stylesheet" media="screen">
<style type="text/css">

#wmd-preview h1  {
    color: #0077bb; /* 将标题改为蓝色 */
}</style>
</head>
<body class="theme theme-white">
<div id="wmd-preview" class="wmd-preview wmd-preview-full-reader"><div class="md-section-divider"></div><div class="md-section-divider"></div><h1 data-anchor-id="lmkh" id="简单工厂设计模式">简单工厂设计模式</h1><hr><div class="md-section-divider"></div><h2 data-anchor-id="wls0" id="简介">简介</h2><p data-anchor-id="aplx">测试又称静态工厂方法模式，是不同的工厂方法模式的一个特殊实现。简单工厂模式就是由一个工厂类根据传入的参量决定创建出哪一种产品类的实例</p><hr><div class="md-section-divider"></div><h2 data-anchor-id="4a62" id="包含角色">包含角色：</h2><div class="md-section-divider"></div><h3 data-anchor-id="og92" id="1工厂角色">1.工厂角色：</h3><p data-anchor-id="eby8">担任这个角色的是工厂方法模式的核心，含有与应用紧密相关的商业逻辑。工厂类在客户端的直接调用下创建产品对象，它往往由一个具体Java类实现。</p><div class="md-section-divider"></div><h3 data-anchor-id="vltb" id="2抽象产品角色">2.抽象产品角色：</h3><p data-anchor-id="qxhr">担任这个角色的类是由工厂方法模式所创建的对象的父类，或它们共同拥有的接口。抽象产品角色可以用一个Java接口或者Java抽象类实现</p><div class="md-section-divider"></div><h3 data-anchor-id="vxpr" id="3具体产品角色">3.具体产品角色：</h3><p data-anchor-id="t7so">工厂方法模式所创建的任何对象都是这个角色的实例，具体产品角色由一个具体Java类实现。</p><div class="md-section-divider"></div><h2 data-anchor-id="5vbn" id="uml类图">uml类图</h2><p data-anchor-id="v1on"><img src="http://chengqj.com/static/images/%E7%AE%80%E5%8D%95%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F.png" alt="类图" title=""></p><hr><div class="md-section-divider"></div><h2 data-anchor-id="mfg9" id="java实现">java实现</h2><div class="md-section-divider"></div><h3 data-anchor-id="wttp" id="1抽象产品角色">1.抽象产品角色</h3><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="xfdr"><ol class="linenums"><li class="L0"><code class="language-java"><span class="com">/**</span></code></li><li class="L1"><code class="language-java"><span class="com"> * @program: pattern</span></code></li><li class="L2"><code class="language-java"><span class="com"> * @description: 抽象产品角色</span></code></li><li class="L3"><code class="language-java"><span class="com"> * @author: chengqj</span></code></li><li class="L4"><code class="language-java"><span class="com"> * @create: 2018-08-06 09:08</span></code></li><li class="L5"><code class="language-java"><span class="com"> **/</span></code></li><li class="L6"><code class="language-java"><span class="kwd">public</span><span class="pln"> </span><span class="kwd">interface</span><span class="pln"> </span><span class="typ">Fruit</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code class="language-java"><span class="pln">    </span><span class="kwd">void</span><span class="pln"> grow</span><span class="pun">();</span></code></li><li class="L8"><code class="language-java"><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="19ac" id="2具体产品角色">2.具体产品角色</h3><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="ht1d"><ol class="linenums"><li class="L0"><code class="language-java"><span class="com">/**</span></code></li><li class="L1"><code class="language-java"><span class="com"> * @program: pattern</span></code></li><li class="L2"><code class="language-java"><span class="com"> * @description: 工厂角色</span></code></li><li class="L3"><code class="language-java"><span class="com"> * @author: chengqj</span></code></li><li class="L4"><code class="language-java"><span class="com"> * @create: 2018-08-06 09:09</span></code></li><li class="L5"><code class="language-java"><span class="com"> **/</span></code></li><li class="L6"><code class="language-java"><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">FruitFactory</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code class="language-java"><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="typ">Fruit</span><span class="pln"> createFruit</span><span class="pun">(){</span></code></li><li class="L8"><code class="language-java"><span class="pln">        </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">AppleFruit</span><span class="pun">();</span></code></li><li class="L9"><code class="language-java"><span class="pln">    </span><span class="pun">}</span></code></li><li class="L0"><code class="language-java"><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h3 data-anchor-id="v8i8" id="3工厂角色">3.工厂角色</h3><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="s9a8"><ol class="linenums"><li class="L0"><code class="language-java"><span class="com">/**</span></code></li><li class="L1"><code class="language-java"><span class="com"> * @program: pattern</span></code></li><li class="L2"><code class="language-java"><span class="com"> * @description: 工厂角色</span></code></li><li class="L3"><code class="language-java"><span class="com"> * @author: chengqj</span></code></li><li class="L4"><code class="language-java"><span class="com"> * @create: 2018-08-06 09:09</span></code></li><li class="L5"><code class="language-java"><span class="com"> **/</span></code></li><li class="L6"><code class="language-java"><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">FruitFactory</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L7"><code class="language-java"><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="typ">Fruit</span><span class="pln"> createFruit</span><span class="pun">(){</span></code></li><li class="L8"><code class="language-java"><span class="pln">        </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">AppleFruit</span><span class="pun">();</span></code></li><li class="L9"><code class="language-java"><span class="pln">    </span><span class="pun">}</span></code></li><li class="L0"><code class="language-java"><span class="pun">}</span></code></li></ol></pre><hr><div class="md-section-divider"></div><h2 data-anchor-id="c0e4" id="优点和缺点">优点和缺点</h2><div class="md-section-divider"></div><h3 data-anchor-id="9tfk" id="优点">优点：</h3><p data-anchor-id="vmfy">客户端需要的什么产品，可以直接得到，逻辑判断在工厂里面</p><div class="md-section-divider"></div><h3 data-anchor-id="m16a" id="缺点">缺点：</h3><p data-anchor-id="1ehj">扩展性一般，产品都是一个产品族下面的产品，对于不在产品族中的产品，没办法去扩展，另外获得具体某种产品的逻辑放在工厂里面，新添加产品的时候就需要修改工厂里面的代码。</p></div>
</body>
</html>